package com.handinglian.ai.record.controller;

import com.apidoc.annotation.Api;
import com.handinglian.ai.common.dto.CommonPage;
import com.handinglian.ai.common.dto.ResultData;
import com.handinglian.ai.notice.param.DtmfCollectParam;
import com.handinglian.ai.record.dto.CallRecordDetailDto;
import com.handinglian.ai.record.dto.CallRecordDto;
import com.handinglian.ai.record.param.PushCallRecordParam;
import com.handinglian.ai.record.param.PushCallRecordPbxParam;
import com.handinglian.ai.record.service.CallRecordService;
import com.handinglian.ai.thirdparty.dto.RobotChatRespDto;
import com.handinglian.ai.thirdparty.dto.YyDtmfCollectDto;
import com.handinglian.ai.thirdparty.service.BaiduAIService;
import lombok.extern.slf4j.Slf4j;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.io.IOException;

@Api("通话记录")
@RestController
@Slf4j
@RequestMapping("/callrecord")
public class CallRecordController {

    @Autowired
    private CallRecordService callRecordService;
    @Autowired
    private BaiduAIService baiduAIService;

    /**
     * 获取通话记录分页列表
     */
    @GetMapping("/listPage")
    public ResultData<CommonPage<CallRecordDto>> listPage(String callerNo, String calledNo, String starTime, String endTime, String recordStatus, String phoneStatus, String sortField, String sortOrder,
                                                          @RequestParam(value = "pageNum", defaultValue = "1") Integer pageNum,
                                                          @RequestParam(value = "pageSize", defaultValue = "10") Integer pageSize) {
        CommonPage<CallRecordDto> commonPage = callRecordService.listPage( starTime, endTime,callerNo, calledNo, recordStatus, phoneStatus, sortField, sortOrder, pageNum, pageSize);
        return ResultData.success(commonPage);
    }

    /**
     * 推送呼叫中心通话记录
     */
    @PostMapping("/pushCdrInfo")
    public String pushCdrInfo(@RequestBody PushCallRecordParam pushCallRecordParam) {
        int amount = callRecordService.pushCdrInfo(pushCallRecordParam.getCdr());
        return amount == 0?"failure":"success";
    }

    /**
     * 推送pbx通话记录
     */
    @PostMapping("/pushCdrPbx")
    public String pushCdrPbx(@RequestBody PushCallRecordPbxParam pbxParam) {
        int amount = callRecordService.pushCdrPbx(pbxParam.getCdr());
        return amount == 0?"failure":"success";
    }

    /**
     * 获取通话详情
     */
    @GetMapping("/getDetail")
    public ResultData<CallRecordDetailDto> getDetail(Integer id) {
        CallRecordDetailDto callRecordDetailDto = callRecordService.getDetail(id);
        return ResultData.success(callRecordDetailDto);
    }

    /**
     * 下载录音
     */
    @GetMapping("/downloadRecording")
    public ResultData downloadRecording(String callid) throws IOException {
        return ResultData.success(callRecordService.download(callid));
    }

    /**
     * 接收通话回拨的按键
     */
    @PostMapping("/pushdtmf")
    public ResultData pushdtmf(String callee, String dtmf, String reqid) {
        log.error("****************"+reqid);
        return ResultData.success(callRecordService.pushdtmf(callee, dtmf, reqid));
    }

    /**
     * 云翌收集按键接口
     */
    @PostMapping("/dtmfCollect")
    public ResultData dtmfCollect(@RequestBody DtmfCollectParam dtmfCollectParam) throws IOException {
        callRecordService.dtmfCollect(dtmfCollectParam);
        return ResultData.success();
    }

    /**
     * 测试
     */
    @GetMapping("/test")
    public ResultData test() throws IOException {
        return ResultData.success("haha");
    }
}
